package com.dkd.manage.service.impl;

import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Hostel;
import com.dkd.manage.domain.Student;
import com.dkd.manage.domain.dto.HostelAssignmentsDTO;
import com.dkd.manage.domain.vo.HostelAssignmentsVo;
import com.dkd.manage.mapper.HostelMapper;
import com.dkd.manage.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.HostelAssignmentsMapper;
import com.dkd.manage.domain.HostelAssignments;
import com.dkd.manage.service.IHostelAssignmentsService;

/**
 * 宿舍分配管理Service业务层处理
 * 
 * @author czq
 * @date 2024-11-09
 */
@Service
public class HostelAssignmentsServiceImpl implements IHostelAssignmentsService 
{
    @Autowired
    private HostelAssignmentsMapper hostelAssignmentsMapper;

    @Autowired
    private HostelMapper hostelMapper;

    @Autowired
    private StudentMapper studentMapper;
    /**
     * 查询宿舍分配管理
     * 
     * @param id 宿舍分配管理主键
     * @return 宿舍分配管理
     */
    @Override
    public HostelAssignments selectHostelAssignmentsById(Long id)
    {
        return hostelAssignmentsMapper.selectHostelAssignmentsById(id);
    }

    /**
     * 查询宿舍分配管理列表
     * 
     * @param hostelAssignments 宿舍分配管理
     * @return 宿舍分配管理
     */
    @Override
    public List<HostelAssignments> selectHostelAssignmentsList(HostelAssignments hostelAssignments)
    {
        return hostelAssignmentsMapper.selectHostelAssignmentsList(hostelAssignments);
    }

    /**
     * 新增宿舍分配管理
     * 
     * @param hostelAssignmentsDTO 宿舍分配管理
     * @return 结果
     */
    @Override
    public int insertHostelAssignments(HostelAssignmentsDTO hostelAssignmentsDTO)
    {
        // 查找是否存在该学生
        Student student = studentMapper.selectStudentByStudentNumber(hostelAssignmentsDTO.getStudentNumber());
        if (student == null) {
            throw new RuntimeException("学生不存在");
        }
        // 查找是否存在该宿舍
        String hostelName = hostelAssignmentsDTO.getHostelName();
        String roomNumber = hostelAssignmentsDTO.getRoomNumber();
        Hostel hostel = hostelMapper.selectHostelByHostelNameAndRoomNumber(hostelName, roomNumber);
        if (hostel == null){
            throw new RuntimeException("宿舍不存在");
        }
        // 宿舍是否有空位 最多 6人
        Integer capacity = hostel.getCapacity();
        if (capacity < 0 || capacity >= 6 ){
            throw new RuntimeException("该宿舍已满");
        }
        // 创建宿舍分配记录
        HostelAssignments hostelAssignments = new HostelAssignments();
        hostelAssignments.setGrade(student.getGrade());
        hostelAssignments.setMajor(student.getMajor());
        hostelAssignments.setStudentId(student.getId());
        hostelAssignments.setHostelId(hostel.getId());
        hostelAssignments.setCreateTime(DateUtils.getNowDate());
        // 宿舍表容纳人数 + 1
        hostel.setCapacity(hostel.getCapacity() + 1);
        hostelMapper.updateHostel(hostel);

        return hostelAssignmentsMapper.insertHostelAssignments(hostelAssignments);
    }

    /**
     * 修改宿舍分配管理
     * 
     * @param hostelAssignments 宿舍分配管理
     * @return 结果
     */
    @Override
    public int updateHostelAssignments(HostelAssignmentsDTO hostelAssignmentsDTO)
    {

        Student student = studentMapper.selectStudentByStudentNumber(hostelAssignmentsDTO.getStudentNumber());
        // 查询该学生的宿舍 ， 宿舍已有人数 -1
        HostelAssignments hostelAssignments = hostelAssignmentsMapper.selectHostelAssignmentsByStudentId(student.getId());
        Hostel hostel = hostelMapper.selectHostelById(hostelAssignments.getHostelId());
        hostel.setCapacity(hostel.getCapacity() - 1);
        hostelMapper.updateHostel(hostel);
        // 查找是否存在修改的宿舍
        String hostelName = hostelAssignmentsDTO.getHostelName();
        String roomNumber = hostelAssignmentsDTO.getRoomNumber();
        Hostel newHostel = hostelMapper.selectHostelByHostelNameAndRoomNumber(hostelName, roomNumber);
        if (newHostel == null){
            throw new RuntimeException("宿舍不存在");
        }
        // 宿舍是否有空位 最多 6人
        Integer capacity = newHostel.getCapacity();
        if (capacity < 0 || capacity >= 6 ){
            throw new RuntimeException("该宿舍已满");
        }

        HostelAssignments newHostelAssignments = new HostelAssignments();
        newHostelAssignments.setId(hostelAssignments.getId());
        // 设置宿舍id
        newHostelAssignments.setHostelId(newHostel.getId());
        newHostelAssignments.setUpdateTime(DateUtils.getNowDate());

        // 宿舍表容纳人数 + 1
        newHostel.setCapacity(newHostel.getCapacity() + 1);
        hostelMapper.updateHostel(newHostel);
        return hostelAssignmentsMapper.updateHostelAssignments(newHostelAssignments);
    }

    /**
     * 批量删除宿舍分配管理
     * 
     * @param ids 需要删除的宿舍分配管理主键
     * @return 结果
     */
    @Override
    public int deleteHostelAssignmentsByIds(Long[] ids)
    {
        return hostelAssignmentsMapper.deleteHostelAssignmentsByIds(ids);
    }

    /**
     * 删除宿舍分配管理信息
     * 
     * @param id 宿舍分配管理主键
     * @return 结果
     */
    @Override
    public int deleteHostelAssignmentsById(Long id)
    {
        return hostelAssignmentsMapper.deleteHostelAssignmentsById(id);
    }

    /**
     * 查询宿舍分配管理列表
     * @param hostelAssignmentsDTO
     * @return HostelAssignmentsVo
     */
    @Override
    public List<HostelAssignmentsVo> selectHostelAssignmentsVoList(HostelAssignmentsDTO hostelAssignmentsDTO) {
        HostelAssignments hostelAssignments = new HostelAssignments();
        String grade = hostelAssignmentsDTO.getGrade();
        if (grade != null){
            hostelAssignments.setGrade(grade);
        }
        // 设置学生id
        String studentNumber = hostelAssignmentsDTO.getStudentNumber();
        Student student = studentMapper.selectStudentByStudentNumber(studentNumber);
        if( student != null) {
            hostelAssignments.setStudentId(student.getId());
        }
        // 设置宿舍id
        String roomNumber = hostelAssignmentsDTO.getRoomNumber();
        Hostel hostel = hostelMapper.selectHostelByRoomNumber(roomNumber);

        if (hostel != null) {
            hostelAssignments.setHostelId(hostel.getId());
        }
        return hostelAssignmentsMapper.selectHostelAssignmentsVoList(hostelAssignments);
    }
}
